package http

import (
	"context"
	"sync"
)

type YHttpKey struct{}

var (
	_instance     IHttpServer
	_instanceLock = sync.Mutex{}
)

func GetInstance() IHttpServer {
	return _instance
}

func WithCtx(ctx context.Context, srv IHttpServer) context.Context {
	_instanceLock.Lock()
	defer _instanceLock.Unlock()

	_instance = srv
	return context.WithValue(ctx, YHttpKey{}, srv)
}

func FromCtx(ctx context.Context) (IHttpServer, bool) {
	val := ctx.Value(YHttpKey{})
	if val == nil {
		return nil, false
	}
	srv, ok := val.(IHttpServer)
	return srv, ok
}
